package com.app.teaware.bean;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.annotation.JSONField;
import com.app.teaware.basis.BasisActivity;
import com.app.teaware.basis.BasisApp;
import com.app.teaware.basis.Constants;
import com.app.teaware.R;
import com.xuexiang.xupdate.XUpdate;
import com.xuexiang.xupdate.entity.UpdateEntity;

import my.LogUtil;
import my.SystemParamsUtils;
import my.http.MyHttpListener;
import my.http.MyHttpUtils;
import my.http.MyParams;


public class UpdateBean extends BasisBean {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    /**
     * APP版本号
     */
    @JSONField(name = "version")
    private int number;
    /**
     * APP下载地址
     */
    @JSONField(name = "url")
    private String url;
    /**
     * APP版本名
     */
    @JSONField(name = "versionShow")
    private String name;
    /**
     * APP版本更新信息
     */
    @JSONField(name = "name")
    private String info;
    @JSONField(name = "forceUp")
    private String is_forced_upgrade = "";

    @JSONField(name = "updateTime")
    private String update_time  = "";
    @JSONField(name = "forcedUpgradeVersion")
    private int forced_upgrade_version;

    // private String time;

    public boolean isNeedUpdate(Context mContext) {
        return this.getNumber() > SystemParamsUtils.getAPPVersonCode(mContext);
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getInfo() {
        if(TextUtils.isEmpty(info))return "无";
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public String getIs_forced_upgrade() {
        return is_forced_upgrade;
    }

    public boolean getIs_forced_upgradeBoolean() {
        return is_forced_upgrade.equals("1");
    }

    public void setIs_forced_upgrade(String is_forced_upgrade) {
        this.is_forced_upgrade = is_forced_upgrade;
    }

    public int getForced_upgrade_version() {
        return forced_upgrade_version;
    }

    public void setForced_upgrade_version(int forced_upgrade_version) {
        this.forced_upgrade_version = forced_upgrade_version;
    }

    public static void check(BasisActivity mContext, final boolean isShowToast) {
        MyParams params = new MyParams();
        params.put("id", "MHJYZHCZ");
        params.put("version", SystemParamsUtils.getAPPVersonCode(mContext));
//        params.put("version", 102);
        if (isShowToast) {
            mContext.showLoading();
        }
        MyHttpUtils.post(Constants.URL_UPDATE_XF, params, new MyHttpListener() {
            @Override
            public void onSuccess(int httpWhat, Object result) {
                UpdateBean updateBean = (UpdateBean) result;
                updateBean.checkUpdate(mContext, isShowToast);
            }

            @Override
            public void onFailure(int httpWhat, Object result) {

                if (isShowToast) {
                    super.onFailure(httpWhat, result);
                }
            }

            @Override
            public void onFinish(int httpWhat) {
                if (isShowToast) {
                    mContext.dismissProgress();
                }
            }
        }, 0, UpdateBean.class);

    }


    public void checkUpdate(Context mContext, boolean showToast) {

        LogUtil.i("getAPPVersonCode---->"+ SystemParamsUtils.getAPPVersonCode(mContext));
        LogUtil.i("getNumber---->"+getNumber());
        if (this.getNumber() > SystemParamsUtils.getAPPVersonCode(mContext)) {
            UpdateEntity updateEntity = new UpdateEntity()
                    .setHasUpdate(isNeedUpdate(mContext))
                    .setForce(getIs_forced_upgradeBoolean())
//                        .setIsIgnorable(updateBean.getIs_forced_upgradeBoolean())
                    .setVersionCode(getNumber())
                    .setVersionName(getName())
                    .setUpdateContent(""+getInfo()+"：\n更新时间："+getUpdate_time())
                    .setDownloadUrl(getUrl())
//                        .setDownloadUrl("https://ugc-download-4.imfir.cn/7257bfd68934ef98eb47f93d218caa934d195800.apk?auth_key=1590048176-0-0-0b5cfbb1355b86d9407472e5f1b85bb8")
//                        .setSize(result.apkSize)
                    ;
            XUpdate.newBuild(mContext)
                    .promptThemeColor(mContext.getResources().getColor(R.color.base_blue))
                    .promptTopResId(R.drawable.xupdate_bg_app_top_blue)
                    .build().update(updateEntity);
        } else {
            if (showToast)
                BasisApp.showToast(R.string.app_already_new);
        }
    }



    public String getUpdate_time() {
        return update_time;
    }

    public void setUpdate_time(String update_time) {
        this.update_time = update_time;
    }
}
